﻿using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using WPF数据采集系统.Base;
using WPF数据采集系统.Model;


namespace WPF数据采集系统.ViewModel
{
    public class MainViewModel : NotifyPropertyBase
    {
        public MainViewModel(string ViewName)
        {
            TabChangedCommand = new CommandBase(OnTabChanged);
            MainViewName = ViewName;
            OnTabChanged(MainViewName);
            MonitorList = new ObservableCollection<LogModel>();
            logListBox = new ObservableCollection<string>();
            RotateList = new ObservableCollection<RotateModel>();
            Language = MainWindow.LANGEUAGE;
        }
        public bool _PlcState;
        public bool PlcState
        {
            get { return _PlcState; }
            set
            {
                _PlcState = value;
                this.RaisePropertyChanged();
            }
        }
        private string _PlcBackGround;
        public string PlcBackGround
        {
            get { return _PlcBackGround; }
            set
            {
                _PlcBackGround = value;
                this.RaisePropertyChanged();
            }
        }
        private string _MesBackGround;
        public string MesBackGround
        {
            get { return _MesBackGround; }
            set
            {
                _MesBackGround = value;
                this.RaisePropertyChanged();
            }
        }
        private string _Language;
        public string Language
        {
            get { return _Language; }
            set
            {
                _Language = value;
                this.RaisePropertyChanged();
            }
        }
        private string _PlcContent;
        public string PlcContent
        {
            get { return _PlcContent; }
            set
            {
                _PlcContent = value;
                this.RaisePropertyChanged();
            }
        }
        private string _MesContent;
        public string MesContent
        {
            get { return _MesContent; }
            set
            {
                _MesContent = value;
                this.RaisePropertyChanged();
            }
        }
        private ObservableCollection<string> _logListBox;
        public ObservableCollection<string> logListBox
        {
            get { return _logListBox; }
            set
            {
                _logListBox = value;
                this.RaisePropertyChanged();
            }
        }
        public ObservableCollection<LogModel> _LogList;
        public ObservableCollection<LogModel> MonitorList
        {
            get { return _LogList; }
            set
            {
                _LogList = value;
                this.RaisePropertyChanged();
            }
        }
        public ObservableCollection<RotateModel> _RotateList;
        public ObservableCollection<RotateModel> RotateList
        {
            get { return _RotateList; }
            set
            {
                _RotateList = value;
                this.RaisePropertyChanged();
            }
        }
        #region 主窗体页面切换
        public string _MainViewName;
        public string MainViewName
        {
            get { return _MainViewName; }
            set { _MainViewName = value; this.RaisePropertyChanged(); }
        }
        private UIElement _mainContent;
        public UIElement MainContent
        {
            get { return _mainContent; }
            set
            {
                Set<UIElement>(ref _mainContent, value);
            }
        }
        public CommandBase TabChangedCommand { get; set; }
        public void OnTabChanged(object obj)
        {
            if (obj == null)
            {
                CustomMsgBox.ShowDialog("通知", "该功能尚在开发中...敬请期待", CustomMsgBoxButton.One, CustomMsgBoxImage.Alert);
                MainWindow.MainViewButton.IsChecked = true;
                OnTabChanged(MainViewName);
                return;
            }
            Type type = Type.GetType(obj.ToString());
            if (type == null)
            {
                CustomMsgBox.ShowDialog("页面不存在", "该页面不存在,请联系系统开发人员处理。。。");
                MainWindow.MainViewButton.IsChecked = true;
                OnTabChanged(MainViewName);
                return;
            }
            switch (obj.ToString())
            {
                case ViewName.MainViewName:
                    //简化方式，必须在同一个程序集下
                    this.MainContent = (UIElement)Activator.CreateInstance(type);//圆柱旋转机
                    break;
                case ViewName.MainViewHotPress:
                    //简化方式，必须在同一个程序集下
                    this.MainContent = (UIElement)Activator.CreateInstance(type);//401热压机
                    break;
                case ViewName.PlcViewName:
                    PagePoweEngineer(type);//工程师以上才能访问
                    break;
                case ViewName.HistoryView:
                    PagePoweEngineer(type);
                    break;
                case ViewName.TesterView:
                    PagePoweEngineer(type);
                    break;
                case ViewName.MesView:
                    PagePoweEngineer(type);
                    break;
                case ViewName.ScanSetView:
                    PagePoweEngineer(type);
                    break;
                case ViewName.SystemSetView://只有管理员方能访问
                    PagePoweAdmin(type);
                    break;
                //case ViewName.CanvasView://只有管理员方能访问
                //    PagePoweEngineer(type);
                //    break;
                default:
                    this.MainContent = (UIElement)Activator.CreateInstance(type);
                    break;
            }
        }
        public void RotateListInIt(int count)
        {
            RotateList = new ObservableCollection<RotateModel>();
            if (MainWindow.RotateModel != null)
            {
                for (int i = 0; i < count; i++)
                {
                    RotateModel rm = new RotateModel();
                    rm.RowNumber = i;
                    rm.RotateTime = "";
                    rm.rotateType = RotateType.None;         
                    RotateList.Add(rm);
                }
            }
        }
        /// <summary>
        ///工程师权限判断
        /// </summary>
        /// <param name="admin_engineer">有权的角色</param>
        /// <param name="type">页面type</param>
        public void PagePoweEngineer(Type type)
        {
            if (MainWindow.LoginRole == "admin" || MainWindow.LoginRole == "engineer")
            {
                this.MainContent = (UIElement)Activator.CreateInstance(type);
            }
            else
            {
                MainWindow.MainViewButton.IsChecked = true;
                CustomMsgBox.ShowDialog("权限不足", "用户:" + MainWindow.LoginUser + " 无权访问当前页面,请联系管理员!", CustomMsgBoxButton.One, CustomMsgBoxImage.Alert);
            }
        }
        /// <summary>
        /// 管理员权限判断
        /// </summary>
        /// <param name="type"></param>
        public void PagePoweAdmin(Type type)
        {
            if (MainWindow.LoginRole == "admin")
            {
                this.MainContent = (UIElement)Activator.CreateInstance(type);
            }
            else
            {
                MainWindow.MainViewButton.IsChecked = true;
                CustomMsgBox.ShowDialog("权限不足", "用户:" + MainWindow.LoginUser + " 无权访问当前页面,请联系管理员!", CustomMsgBoxButton.One, CustomMsgBoxImage.Alert);
            }
        }
        #endregion
    }
}
